package com.example.howard.picview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/9/12.
 */
public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<String> imagepath = new ArrayList<>();
    private List<String> imagetime = new ArrayList<>();
    public boolean isState = false;
    private MyApplication myApplication;
    LayoutInflater inflater;

    public MyAdapter(List<String> imagepath, List<String> imagetime,Context context, MyApplication myApplication) {
        this.imagepath = imagepath;
        this.imagetime=imagetime;
        this.context=context;
        this.myApplication=myApplication;
        this.inflater = LayoutInflater.from(context);
    }

    public void setData (List<String> imagepath, List<String> imagetime){
        this.imagepath = imagepath;
        this.imagetime=imagetime;
    }

    public void setIsState(boolean isState) {
        this.isState = isState;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return imagepath.size();
    }

    @Override
    public Object getItem(int position) {
        return imagepath.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_tab1_item, null);
            viewHolder = new ViewHolder();


            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
            viewHolder.Image= (ImageView) convertView.findViewById(R.id.picviewitem);
            viewHolder.time= (TextView) convertView.findViewById(R.id.madetime);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ArrayList<Boolean> list = myApplication.getSelectItems();
//        convertView = inflater.inflate(resourse ,null);
        String path = imagepath.get(position);
        String time=imagetime.get(position);
//        String time=timeall.substring(0,10);

        Picasso
                .with(context)
                .load(new File(path))
                .resize(300,300)
                .centerCrop()
                .placeholder(R.drawable.picassopic)
                .error(R.drawable.picassopic)
                .into(viewHolder.Image);
//            Picasso.with(this.context).load(new File(path)).into(holder.Image);
        viewHolder.time.setText(time);
        viewHolder.checkBox.setVisibility(isState? View.VISIBLE : View.GONE);
        if (list.size() != 0)
            viewHolder.checkBox.setChecked(list.get(position));


        viewHolder.checkBox.setVisibility(isState? View.VISIBLE : View.GONE);
        if (list.size() != 0)
            viewHolder.checkBox.setChecked(list.get(position));



        return convertView;
    }

    class ViewHolder {
         CheckBox checkBox;
         ImageView Image;
         TextView time;
    }
}
